package logic

import (
	"context"
	"fmt"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShowProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShowProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShowProductLogic {
	return &ShowProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShowProductLogic) ShowProduct(in *productrpc.ShowProductRequest) (*productrpc.ShowProductResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	err := mysql.DB.Debug().Where("id = ?", in.Id).Find(&product).Error
	if err != nil {
		return nil, fmt.Errorf("商品详情查看失败")
	}

	product.BrowseNumber = product.BrowseNumber + 1

	mysql.DB.Debug().Updates(&product)

	return &productrpc.ShowProductResponse{
		Id:           int64(product.ID),
		Title:        product.Title,
		Price:        float32(product.Price),
		Image:        product.Image,
		ClassId:      product.ClassId,
		BrowseNumber: product.BrowseNumber,
	}, nil
}
